package com.xlh.enums.course;

import com.google.common.collect.Lists;
import com.xlh.dto.course.ChapterParentDTO;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 章节存储的方式
 * <p>
 * Created by lx on 2019/1/28.
 */
@Getter
@AllArgsConstructor
public enum ChapterEnum {

    CHAPTER(1, "章节") {
        @Override
        public <T extends ChapterParentDTO> List<T> getConvertData(List<T> data) {
            if (CollectionUtils.isEmpty(data)) return data;
            return data.stream()
                    .filter(chapterInfo -> chapterInfo.getParentId() == null
                            || chapterInfo.getParentId() == 0).collect(Collectors.toList());
        }
    },
    TASK(2, "任务") {
        @Override
        public <T extends ChapterParentDTO> List<T> getConvertData(List<T> data) {

            if (CollectionUtils.isEmpty(data)) return data;

            // 获取所有章节（已排序）
            List<T> chapterData = ChapterEnum.CHAPTER.getConvertData(data);

            // 获取所有任务（未排序）
            List<T> taskData = data.stream()
                    .filter(chapterInfo -> chapterInfo.getParentId() != null
                            && chapterInfo.getParentId() > 0).collect(Collectors.toList());

            if (CollectionUtils.isEmpty(chapterData)) {
                return taskData;
            }

            // 将任务排序
            List<T> result = Lists.newArrayList();
            chapterData.forEach(t -> {
                List<T> tasks = taskData.stream()
                        .filter(t1 -> Objects.equals(t1.getParentId(), t.getId())).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(tasks)) return;
                result.addAll(tasks);
            });
            return result;
        }
    };

    private int code;

    private String message;

    public <T extends ChapterParentDTO> List<T> getConvertData(List<T> chapterBasics) {
        return chapterBasics;
    }

}
